<template>
  <view class="cart-container" v-if="JSON.stringify(cart)!=='[]'">
    <!-- 收货地址区域 -->
    <my-address></my-address>
    <!-- 购物车标题区域 -->
    <view class="cart-title">
      <uni-icons type="shop" size="18"></uni-icons>
      <text class="cart-title-text">购物车</text>
    </view>
    <!-- 购物车商品信息列表 -->
    <view class="cart-list">
      <uni-swipe-action>
        <block v-for="(goods,index) of cart" :key="index">
          <uni-swipe-action-item :options="options"  @click="deleteGoods(goods)">
            <my-goods :goods="goods" :showRadio="true" :showNumber="true" @radio="changeHandler" @numChange="changeCount"></my-goods>
          </uni-swipe-action-item>
        </block>
      </uni-swipe-action>
      <!-- 自定义结算组件 -->
      <my-settle></my-settle>
    </view>
  </view>
  <!-- 空白购物车区域 -->
  <view class="empty-box" v-else>
    <image src="/static/cart_empty@2x.png" ></image>
    <text>空空如也~</text>
  </view>
</template>

<script>
  import badgeMix from '../../mixins/tabbar-badge.js'
  import {
    mapState,
    mapMutations
  } from 'vuex'
  export default {
    mixins: [badgeMix],
    computed: {
      //获取购物车的信息：
      ...mapState('m_cart', ['cart'])
    },
    data() {
      return {
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#dd524d'
          }
        }]
      };
    },
    methods: {
      ...mapMutations('m_cart', ["updateState", "updateCount","cutGoods"]),
      changeHandler(e) {
        //对商品的状态进行修改
        this.updateState(e)
      },
      changeCount(goods) {
        //console.log(goods)
        //对商品的数量进行更新
        this.updateCount(goods)
      },
      deleteGoods(goods){
        //删除购物车的商品信息：
        this.cutGoods(goods)
      }
    }

  }
</script>

<style lang="scss">
  .cart-container{
    padding-bottom: 50px;
  }
  .cart-title {
    height: 40px;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding-left: 5px;
    border-bottom: 1px solid #efefef;

    .cart-title-text {
      margin-left: 10px;
    }
  }
  .empty-box{
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 150px;
    image{
      width: 90px;
      height: 90px;
    }
    text{
      margin-top: 15px;
      color: gray;
      font-size: 12px;
    }
  }
</style>
